<?php
die('数据已执行完毕，不在运行');
$dbms='mysql';     //数据库类型
$host='localhost'; //数据库主机名
$dbName='copton';    //使用的数据库
$user='root';      //数据库连接用户名
$pass='root';          //对应的密码
$dsn="$dbms:host=$host;dbname=$dbName";

$res = [
    'status'=>0
];
if(!empty( $_POST ) ){
    $brand = $_POST['brand'];

    $brand_s = str_replace('\\','', $brand );
    $std = json_decode( $brand_s, true );
//    echo $brand;
//    print_r( $std );

    try {
        $dbh = new PDO($dsn, $user, $pass); //初始化一个PDO对象

        $brand_sql = "insert into brand set id='{$std['brandid']}', `name`='{$std['brandname']}',letter='{$std['brandletter']}'";
        $series_sql = 'insert into series VALUES ';
        $model_sql = 'insert into model VALUES ';
        $series_sql_value = '';
        $model_sql_value = '';
        if( !empty( $std['children']) ){
            $brand_children = $std['children'];
            foreach( $brand_children  as $key => $series ){
                $series_sql_value .= !empty( $series_sql_value ) ? ',':'';
                $series_sql_value .= "('{$series['seriesid']}','{$std['brandid']}','{$series['seriesname']}')";

                if( !empty($series['children']) ){
                    $series_children = $series['children'];
                    foreach( $series_children as $k => $model ){
                        $model_sql_value .= !empty( $model_sql_value ) ? ',':'';
                        $model_sql_value .= "('{$model['modelsid']}','{$series['seriesid']}','{$model['year']}','{$model['modelsname']}')";
                    }
                }

            }
        }



        $series_sql .= $series_sql_value;
        $model_sql .= $model_sql_value;
        $res['brand']   = $dbh->query( $brand_sql );
        $res['series']  = $dbh->query( $series_sql );
        $res['model']   = $dbh->query( $model_sql );

        /*你还可以进行一次搜索操作
        foreach ($dbh->query('SELECT * from FOO') as $row) {
            print_r($row); //你可以用 echo($GLOBAL); 来看到这些值
        }
        */
        $dbh = null;
    } catch (PDOException $e) {
        $res['status'] = 1;
        $res['error'] = "Error!: " . $e->getMessage() . "<br/>";
//        die ("Error!: " . $e->getMessage() . "<br/>");
    }
}

echo json_encode( $res );
